package com.jwpdk.appframework.Filter;


import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.jwpdk.appframework.controller.Controller;
import com.jwpdk.appframework.controller.Jwpdk;
import com.jwpdk.appframework.controller.JwpdkRequest;
import com.jwpdk.common.Config;
import com.jwpdk.common.Constant;

/**
 * 过滤器
 * @author 王志伟
 */
public class JwpdkBaseFilter implements Filter{
	private final Logger log	= Logger.getLogger(this.getClass());
	//private static String rootpath ="";	//系统根目录
	//private static String language="language/";
	private static Config config= Config.getInstance();
	public void destroy() {
	}
	
	
	
	/**
	 * 请求拦截
	 */
	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
		
		HttpServletRequest request = new JwpdkRequest( servletRequest);
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		/*
		 * post 方式 form提交 jquery ajax
		 * get方式	from	jquery ajax  URL
		 * get方式（UTF8和gbk） url提交 IE chrome效果不同
		System.out.println(request.getCharacterEncoding());
		 
		if(request.getCharacterEncoding()!=null)
			encoding=request.getCharacterEncoding();
		else 
			encoding = config.getEncode();*/
		//String a1 = request.getParameter("a1");
		//if(null!=a1)
		//System.out.println(new String(a1.getBytes("UTF-8")));
		// 处理中文问题
		/*if (request.getMethod().equals("POST")) {  
		    request.setCharacterEncoding(encoding);
		    response.setContentType("text/html;charset="+encoding);
		} else {  
			request = new JwpdkRequest(request,encoding);
		}  */
		
		Controller controller =  new Controller(request,response);
	    String servletPath = request.getServletPath();
	    if(servletPath.indexOf(".") > 0|| servletPath.equals("/")){
	    	String extension = servletPath.substring(servletPath.indexOf(".")+1, servletPath.length());
	    	//非action请求
	    	if(!config.getActionExtension().equals(extension)){
	    		
	    		//调用资源文件。暂时直接调用。后期可做缓存化处理
	    		if(Constant.RESOURCE_FILE_SUFFIX.indexOf(String.format("|%s|",extension))>0){
	    			chain.doFilter(request,response);
	    		//TAG请求，后期可实现指定类型 不能按照后缀为JSP进行匹配	
	    		}else if(Constant.TAG_SUFFIX.indexOf(String.format("|%s|",extension))>0){
	    			System.err.println(servletPath);
			    	controller.tagService(chain);
	    		}else {
    				chain.doFilter(request,response);
    			}
    			return ;
	    	}
	    }
	    //action 请求
	    try {
	    	System.err.println(servletPath);
			controller.actionService();
		} catch (Exception e) {
			log.error(e);
			e.printStackTrace();
		}
	    
	  /*  int startIndex = request.getContextPath().equals("") ? 0 : request.getContextPath().length();
		String requestUri = request.getRequestURI();
	    int endIndex = request.getPathInfo() == null ? requestUri.length() : requestUri.lastIndexOf(request.getPathInfo());
	    if (startIndex > endIndex) { // this should not happen
	    	endIndex = startIndex;
	    }

	        servletPath=   requestUri.substring(startIndex, endIndex);
	        System.out.println("1="+servletPath);
	        System.out.println("2="+request.getPathInfo());*/
	        
	   
	       
	}
	public void init(FilterConfig arg0) throws ServletException {
		try {
			Jwpdk.initialization();
		} catch ( Exception e ) {
			log.error(e);
			e.printStackTrace();
			throw new ServletException("初始框架信息失败！！");
		}
	}
	
}

